import multiprocessing

import time
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor


def task(num):
    print("执行前", num)
    time.sleep(2)
    print("执行后")


def done(res):
    # hook是由主进程来执行，这点与线程逻辑不一样
    print(multiprocessing.current_process())
    print(multiprocessing.current_process().pid)
    time.sleep(1)
    print(res.result())
    time.sleep(1)


if __name__ == '__main__':
    # spawn模式，参数可以传进程锁，不能传线程锁
    # lock_object = multiprocessing.RLock()
    # manager = multiprocessing.Manager
    # manager.RLock = lock_object # 锁法不一样
    multiprocessing.set_start_method("fork")
    # 进程池中的子进程sleep会自动切换，进入其他子进程；因为是并行，自然是非阻塞
    pool = ProcessPoolExecutor(4)
    for i in range(10):
        fur = pool.submit(task, i)
        fur.add_done_callback(done)
    print(1)
    print(2)
    pool.shutdown(True)
    print("True End....")